/**
 * Created by SylarSong on 16/12/7.
 */



//************获取上一个月***************
function getPreMonth(date) {
    /**
     * @param: data    例如  new Date()
     * @param: return  时间戳  例如  new Date().getTime()
     * **/
    if (!date)date = new Date();
    var year = date.getFullYear(); //获取当前日期的年份
    var month = date.getMonth(); //获取当前日期的月份
    var day = date.getDate(); //获取当前日期的日
    var oneDay = 1000 * 3600 * 24 * 1;
    var days = new Date(year, month, 0).getDate(); //获取当前日期中月的天数
    var current_time = date.getTime();
    var after_time = current_time - (oneDay * days);
    return after_time;
}
//************获取当前以前N天日期信息***************
function getPerDay(n, cycle) {
    if (!cycle)cycle = 1;
    var res = [];
    var oneDay = 1000 * 3600 * 24 * cycle;
    var now = new Date().getTime();
    for (var i = 0; i < n; i++) {
        res.push([new Date(now).getMonth() + 1, new Date(now).getDate()].join("-"))
        now -= oneDay;
    }
    return res.reverse()
}

//************制作N个假数据***************
function getRandomData(n, f, multiple) {
    if (!multiple)multiple = 1;
    var res = [];
    for (var i = 0; i < n; i++) {
        res.push(Math.floor(Math.random() * f) * multiple);
    }
    return res.reverse();
}
//************获取图标实例******************
var bar_arpu_arppu = echarts.init($('#bar-arpu-arppu')[0]); // 次日留存率+新增用户数

//!!!!!!!!!!!!!!!准备时间!!!!!!!!!!!!!
$(document).ready(function () {
    get_arpu_arppu(bar_arpu_arppu, getPerDay(30, 1), getRandomData(30, 1000), getRandomData(30, 1000), getRandomData(30, 200));
    //日按钮时间监听
    $('#btn-newPay-allPay-day').click(function () {
        get_arpu_arppu(bar_arpu_arppu, getPerDay(30, 1), getRandomData(30, 1000), getRandomData(30, 1000), getRandomData(30, 200));
    });
    //周按钮时间监听
    $('#btn-newPay-allPay-week').click(function () {
        get_arpu_arppu(bar_arpu_arppu, getPerDay(15, 7), getRandomData(15, 1000, 7), getRandomData(15, 1000, 7), getRandomData(15, 200, 7));
    });
    //月按钮时间监听
    $('#btn-newPay-allPay-month').click(function () {
        bar_arpu_arppu = echarts.init($('#bar-arpu-arppu')[0])
        get_arpu_arppu_month(bar_arpu_arppu)
    });
});

//!!!!!!!!改变尺寸触发!!!!!!!!!!!!
window.onresize = function () {
    $('#bar-arpu-arppu').width($(".grid-12") - 50);
    bar_arpu_arppu.resize();
};


//*计算 arpu 和 arppu 图表 (按周 , 按日)********************************************
/**
 * 计算 arpu 和 arppu 图表 (按周 , 按日)
 * @param instance
 * @param date  日期数据
 * @param webchat_data 微信支付数据
 * @param aliPay_data  支付宝支付数据
 * @param unionPay_data  银联支付数据
 */
function get_arpu_arppu(instance, date, webchat_data, aliPay_data, unionPay_data) {
    option = {
        tooltip: {
            trigger: 'axis',
            axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
            }
        },
        legend: {
            data: ['微信', '支付宝', '银联']
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        xAxis: {
            type: 'category',
            data: date
        },
        yAxis: {
            type: 'value'
        },
        series: [
            {
                name: '微信',
                type: 'bar',
                stack: '总量',
                label: {
                    normal: {
                        show: true,
                        position: 'insideRight'
                    }
                },
                data: webchat_data
            },
            {
                name: '支付宝',
                type: 'bar',
                stack: '总量',
                label: {
                    normal: {
                        show: true,
                        position: 'insideRight'
                    }
                },
                data: aliPay_data
            },
            {
                name: '银联',
                type: 'bar',
                stack: '总量',
                label: {
                    normal: {
                        show: true,
                        position: 'insideRight'
                    }
                },
                data: unionPay_data
            },
        ],
        color: ["#348903", "#108ee9", "#ff2222"]
    };
    instance.setOption(option);
}

//*计算 arpu 和 arppu 图表 (按月)********************************************
function get_arpu_arppu_month(instance, data) {

    option = {
        title: {
            text: '本月支付渠道占比',
            subtext: '微信|支付宝|银联',
            x: 'center'
        },
        tooltip: {
            trigger: 'item',
            formatter: "{a} <br/>{b} : {c} ({d}%)"
        },
        legend: {
            orient: 'vertical',
            left: 'left',
            data: ['微信', '支付宝', '银联']
        },
        series: [
            {
                name: '支付来源',
                type: 'pie',
                radius: '55%',
                center: ['50%', '60%'],
                data: [
                    {value: 335, name: '微信'},
                    {value: 310, name: '支付宝'},
                    {value: 234, name: '银联'},
                ],
                itemStyle: {
                    emphasis: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }
        ]
    };
    instance.setOption(option);
}